'use strict';

const Controller = require('egg').Controller;
const Op = require('sequelize').Op;
class ProductController extends Controller {
  async getProduct() {
    const {
      ctx,
    } = this;
    const name1 = ctx.params.name1;
    const result = await ctx.service.productInfo.getProduct(name1);
    ctx.body = result;
  }
  async getProducts() {
    const {
      ctx,
    } = this;
    const name1 = ctx.params.name1;
    const name2 = ctx.params.name2;
    const result = await ctx.service.productInfo.getProducts(name1, name2);
    ctx.body = result;
  }
  async productId() {
    const {
      ctx,
    } = this;
    const id = ctx.params.id;
    const result = await ctx.service.productInfo.selId(id);
    ctx.body = result;
  }
  async getTypeProduct() {
    const {
      ctx,
    } = this;
    const name = ctx.params.name;
    const result = await ctx.model.Productinfo.findAll({
      where: {
        [Op.or]: {
          type: {
            [Op.like]: `%${name}%`,
          },
          productname: {
            [Op.like]: `%${name}%`,
          },
        },
        [Op.and]: {
          tuji: true,
        },
      },
      limit: 4,
      order: [
        [ 'createdDate', 'DESC' ],
      ],
    });
    if (result) {
      ctx.body = {
        status: 200,
        data: {
          result,
        },
      };
      return;
    }
    ctx.body = {
      status: 400,
      data: {
        msg: '数据获取失败请重试',
      },
    };
  }
  async getTypeAllProduct() {
    const {
      ctx,
    } = this;
    const name = ctx.params.name;
    const result = await ctx.service.productInfo.getTypeProductInfo(name);
    ctx.body = result;
  }
  async getPriceProduct() {
    const {
      ctx,
    } = this;
    const min = ctx.params.min;
    const max = ctx.params.max;
    const type = ctx.params.type;
    const result = await ctx.model.Productinfo.findAll({
      where: {
        price: {
          [Op.gt]: min,
          [Op.lt]: max,
        },
        [Op.and]: {
          productname: {
            [Op.like]: `%${type}%`,
          },
        },
      },
    });
    if (result) {
      ctx.body = {
        status: 200,
        data: result,
      };
      return;
    }
    ctx.body = {
      status: 400,
    };
  }
  async getProductName() {
    const {
      ctx,
    } = this;
    const name = ctx.params.name;
    const type = ctx.params.type;
    const result = await ctx.service.productInfo.getProductInfo(name, type);
    ctx.body = result;
  }
  async getType() {
    const {
      ctx,
    } = this;
    const result = await ctx.model.Producttype.findAll();
    if (result) {
      ctx.body = {
        status: 200,
        data: {
          result,
        },
      };
      return;
    }
    ctx.body = {
      status: 400,
      data: {
        msg: '数据获取失败请重试',
      },
    };
  }
  async productchildtype() {
    const {
      ctx,
    } = this;
    const id = ctx.params.id;
    const resutl = await ctx.model.query(`select productImg, type from productinfo where tid like ${id} and (type is not null and type != '')`);
    if (resutl) {
      ctx.body = {
        status: 200,
        data: resutl[0],
      };
      return;
    }
    ctx.body = {
      status: 400,
    };
  }
}

module.exports = ProductController;
